﻿using System.Windows;
using GalaSoft.MvvmLight.Threading;
using Greg.WPF.Utility;

namespace SessionHunter
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static App()
        {
            DispatcherHelper.Initialize();
        }

        /// <summary>
        /// Gets or sets a value indicating whether the exception was handled
        /// </summary>
        /// <value>
        ///   <c>true</c> if [do handle]; otherwise, <c>false</c>.
        /// </value>
        public bool DoHandle { get; set; }
        /// <summary>
        /// Handles unhandled exceptions and displays them on the screen
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Threading.DispatcherUnhandledExceptionEventArgs"/> instance containing the event data.</param>
        /// <remarks>From http://www.codeproject.com/Articles/90866/Unhandled-Exception-Handler-For-WPF-Applications and http://exceptionbox.codeplex.com/</remarks>
        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            if (this.DoHandle)
            {
                //Handling the exception within the UnhandledException handler.
                ExceptionMessageBox window = new ExceptionMessageBox(e.Exception, "An unhandled exception has occured.");
                window.ShowDialog();
                e.Handled = true;
            }
            else
            {
                //If you do not set e.Handled to true, the application will close due to crash.
                ExceptionMessageBox window = new ExceptionMessageBox(e.Exception, "An unhandled exception has occured - Application will need to close.");
                window.ShowDialog();
                e.Handled = false;
            }
        }
    }
}
